import java.util.Arrays;

public class Test {
    //关于合并两个非降序数组的方法
    //1.合并后再排序
    public void merge1(int[] nums1, int m, int[] nums2, int n) {
        for (int i = 0; i < n; i++) {
            nums1[m+i] = nums2[i];
        }
        Arrays.sort(nums1);
    }

    public static void main1(String[] args) {
        int m = 5;
        int n = 5;
        int[] nums1 = {1,2,4,6,7,0,0,0,0,0};
        int[] nums2 = {1,2,2,4,8};
        Test test = new Test();
        test.merge1(nums1,m,nums2,n);
        System.out.println(Arrays.toString(nums1));
    }

    //2.使用双指针进行排序
    public void merge2(int[] nums1, int m, int[] nums2, int n){
        while(m > 0 && n > 0){
            if(nums2[n-1] > nums1[m-1]){
                nums1[m+n-1] = nums2[n-1];
                n--;
            }else{
                nums1[m+n-1] = nums1[m-1];
                m--;
            }
        }
        while(n > 0){
            nums1[n-1] = nums2[n-1];
            n--;
        }
    }

    public static void main(String[] args) {
        int m = 5;
        int n = 5;
        int[] nums1 = {1,2,4,6,7,0,0,0,0,0};
        int[] nums2 = {1,2,2,4,8};
        Test test = new Test();
        test.merge2(nums1,m,nums2,n);
        System.out.println(Arrays.toString(nums1));
    }
}


